package com.orcs.leefei.utils.kotlin

import android.annotation.SuppressLint
import android.app.Activity
import android.graphics.Point
import android.graphics.Rect
import android.support.annotation.ColorInt
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.Window
import android.support.v4.app.Fragment as SupportFragment


inline fun Activity.statusBarColor(@ColorInt color: Int) {
    statusBarColor(window, color)
}

inline fun SupportFragment.statusBarColor(@ColorInt color: Int) {
    statusBarColor(activity?.window, color)
}

@SuppressLint("NewApi")
fun statusBarColor(window: Window?, @ColorInt color: Int) {
    if (window == null) return;
    if (lollipopOrNewer()) {
        window.statusBarColor = color
    }
}

/**
 * exclude StatusBarHeight
 */
fun Activity.displayFrame(view: View?): Rect {
    val rect = Rect()
    view?.getWindowVisibleDisplayFrame(rect);
    return rect;
}

fun SupportFragment.displayFrame(view: View?): Rect {
    return (activity as AppCompatActivity).displayFrame(view)
}

/**
 * exclude StatusBarHeight
 */
fun Activity.displayFrame(activity: AppCompatActivity): Rect {
    val rect = Rect()
    activity.window.decorView.getWindowVisibleDisplayFrame(rect);
    return rect;
}

fun Activity.navigationBarHeight(): Int {
    val resourceId = resources.getIdentifier(
            "navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    }
    return 0;
}

fun Activity.navigationView(): View? {
    return window.decorView.findViewById(android.R.id.navigationBarBackground)
}

fun Activity.statusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId)
    }
    return 0
}

fun SupportFragment.statusBarHeight(): Int {
    val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId)
    }
    return 0
}

val SupportFragment.navigationView
    get() = activity.window.decorView.findViewById(android.R.id.navigationBarBackground)
val AppCompatActivity.navigationView
    get() = window.decorView.findViewById(android.R.id.navigationBarBackground)


val displaySizePoint = Point()

fun screenWidth(): Int {
    resolveDisplaySize()
    return if (isLandscape()) displaySizePoint.y else displaySizePoint.x
}

fun screenHeight(): Int {
    resolveDisplaySize()
    return if (isLandscape()) displaySizePoint.x else displaySizePoint.y
}


fun resolveDisplaySize() {
    if (displaySizePoint.x != 0) {
        return
    }
    val w = Orcs.context.windowManager
    if (w != null) {
        if (icsOrNewer()) {
            w.defaultDisplay.getSize(displaySizePoint);
        } else {
            val d = w.getDefaultDisplay()
            displaySizePoint.x = d.getWidth()
            displaySizePoint.y = d.getHeight()
        }

        if (displaySizePoint.x > displaySizePoint.y) {
            val temp = displaySizePoint.x
            displaySizePoint.x = displaySizePoint.y
            displaySizePoint.y = temp
        }
    }
}